package com.ztom.offer.v2;

/**
 * 二维数组中的查找
 * <p>
 * https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/
 *
 * @author ZhangTao
 */
public class Code02FindNumberIn2DArray {

    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
            return false;
        }

        int m = matrix.length;
        int n = matrix[0].length;
        // 从右上角出发
        int r = 0;
        int c = n - 1;
        int cur = 0;
        while (r < m && c >= 0) {
            cur = matrix[r][c];
            if (cur == target) {
                return true;
            } else if (cur > target) {
                c--;
            } else {
                r++;
            }
        }
        return false;
    }
}
